package cn.daniellee.plugin.wc;

import cn.daniellee.plugin.wc.command.CheckerCommand;
import cn.daniellee.plugin.wc.listener.PlayerListener;
import cn.daniellee.plugin.wc.model.Confirm;
import cn.daniellee.plugin.wc.model.WorldNeedCheck;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;

public class WorldChecker extends JavaPlugin {

	private static WorldChecker instance;

	private String prefix;

	private List<WorldNeedCheck> worldNeedChecks = new Vector<>();

	private Map<String, Confirm> confirmMap = new HashMap<>();

	public void onEnable(){
		instance = this;

		loadConfig();
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> WorldChecker Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
		getLogger().info(" ");

		Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
		Bukkit.getPluginCommand("worldchecker").setExecutor(new CheckerCommand());
	}

	public void loadConfig() {
		getLogger().info("[WorldChecker] Loading config...");
		if(!getDataFolder().exists()) getDataFolder().mkdirs();
		prefix = "&7[&b" + getConfig().getString("prompt-prefix", "WorldChecker") + "&7] &3: &r";
		// 更新受限的世界
		worldNeedChecks.clear();
		ConfigurationSection worldSection = getConfig().getConfigurationSection("world-need-check");
		if (worldSection != null) {
			Set<String> worldNames = worldSection.getKeys(false);
			for (String worldName : worldNames) {
				WorldNeedCheck worldNeedCheck = new WorldNeedCheck();
				worldNeedCheck.setWorldName(worldName);
				worldNeedCheck.setPermission(getConfig().getString("world-need-check." + worldName + ".permission", "worldchecker." + worldName));
				worldNeedCheck.setAllowPaid(getConfig().getBoolean("world-need-check." + worldName + ".allow-paid", true));
				worldNeedCheck.setCostType(getConfig().getString("world-need-check." + worldName + ".cost-type", "Vault"));
				worldNeedCheck.setCostPerJoin(getConfig().getDouble("world-need-check." + worldName + ".cost-per-join", 1000D));
				worldNeedChecks.add(worldNeedCheck);
			}
		}
		saveDefaultConfig();
	}

	@Override
	public void onDisable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> WorldChecker Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}

	public static WorldChecker getInstance() {
		return instance;
	}

	public String getPrefix() {
		return prefix;
	}

	public List<WorldNeedCheck> getWorldNeedChecks() {
		return worldNeedChecks;
	}

	public Map<String, Confirm> getConfirmMap() {
		return confirmMap;
	}

	public WorldNeedCheck getWorldNeedCheckByWorldName(String worldName) {
		for (WorldNeedCheck worldNeedCheck : worldNeedChecks) {
			if (worldNeedCheck.getWorldName().equalsIgnoreCase(worldName)) {
				return worldNeedCheck;
			}
		}
		return null;
	}

}